//
//  MyCollectShopDemandTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2019/1/21.
//  Copyright © 2019年 Michael Li. All rights reserved.
//

#import "MyCollectShopDemandTabVC.h"
#import "DemandCell.h"
#import "WriteRequirementTitleCell.h"
#import "ShopOnlineOfflineCell.h"
#import "ShopSubModel.h"
#import "HomeModel.h"
@interface MyCollectShopDemandTabVC ()<DemandCellDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary *dict;

@property(nonatomic,strong) MJRefreshAutoNormalFooter *footer;
@end
static NSString *identifierShop = @"shopCell";
static NSString *identifierShopOnOFF = @"shopOnOffCell";
static NSString *identifierWriteRequirementTitleCell = @"WriteRequirementTitleCell";
static NSString *identifierEvaluation = @"evaluationCell";
static NSString *identifierService = @"ServiceCell";
static NSString *identifierNeed = @"DemandCell";
@implementation MyCollectShopDemandTabVC
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
        
    }
    return _demandArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
    self.tableView.mj_footer = self.footer;
    [self.footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
                 forState:MJRefreshStateNoMoreData];
    self.footer.stateLabel.font = [UIFont systemFontOfSize:12];
    self.footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellReuseIdentifier:identifierShop];
    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierWriteRequirementTitleCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShopOnlineOfflineCell" bundle:nil] forCellReuseIdentifier:identifierShopOnOFF];
    [self.tableView registerNib:[UINib nibWithNibName:@"DemandCell" bundle:nil] forCellReuseIdentifier:identifierNeed];
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceCell" bundle:nil] forCellReuseIdentifier:identifierService];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(demandselectDemandByUid:) name:@"demandselectDemandByUid" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
    [self.tableView registerNib:[UINib nibWithNibName:@"EvaluationCell" bundle:nil] forCellReuseIdentifier:identifierEvaluation];
    self.page = 1;
    self.row = 10;
    [self reloadInternetData];
}
-(void)reloadInternetData {
    
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    NSString *uids = [NSString stringWithFormat:@"%ld",self.model.uid];
    NSString *pages = [NSString stringWithFormat:@"%ld",self.page];
    NSString *rows = [NSString stringWithFormat:@"%ld",self.row];
        self.dict = @{
                      @"uid":[UserDefaultsHelper readUid],
                      @"pageNum":pages,
                      @"pageSize":rows,
                      @"token":[UserDefaultsHelper readToken],
                      @"suid":uids
                      };
        XLog(@"self.dict:%@",self.dict);
        [CustomeManager POSTWithURLIndex:30 parameters:self.dict success:^(NSDictionary *dict) {
            
        } failure:^(NSError *error) {
            
        }];
        
    
}
#pragma mark-代理:查询需求
-(void)demandselectDemandByUid:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"查询需求dict:%@",dict);
    NSArray *date = [dict objectForKey:@"date"];
    XLog(@"date.count:%ld",date.count);
    if (date.count) {
        for (NSDictionary *dict in date) {
            HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
            [self.demandArray addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
    }
    if (self.demandArray.count) {
        if (date.count < 10) {
            self.footer.hidden = NO;
            XLog(@"MJRefreshStateNoMoreData");
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
    }else {
        self.footer.hidden = YES;
        [self.tableView.mj_header endRefreshing];
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }
    XLog(@"self.demandArray:%@",self.demandArray);
    [self.tableView reloadData];
    [WSProgressHUD dismiss];
}

-(void)reloadMore
{
    self.page ++;
    [self reloadInternetData];
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.demandArray.count?self.demandArray.count+1:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.row == 0) {
        WriteRequirementTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierWriteRequirementTitleCell forIndexPath:indexPath];
        cell.titleL.text = @"在销售服务";
        return cell;
    }else {
        DemandCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierNeed forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.collectShow = NO;
        cell.indexPath = indexPath;
        if (self.demandArray.count) {
            HomeModel *model = self.demandArray[indexPath.row-1];
            cell.delegate = self;
            cell.homeModel = model;
        }
        cell.delegate = self;
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     HomeModel *model = self.demandArray[indexPath.row-1];
    if (self.returnValueBlock) {
        self.returnValueBlock(model);
    };
}
#pragma mark-代理:显示联系人
-(void)demandCell:(DemandCell *)demandCell withClickBtn:(UIButton *)clickBtn withIndexPath:(NSIndexPath *)indexPath withHomeModel:(HomeModel *)homeModel
{
    HomeModel *model = self.demandArray[indexPath.row-1];
    model.show = clickBtn.selected;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//    [self.tableView reloadData];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 45;
    }else {
        return 144;
    }
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
@end
